function loadJs(url,callback){
    var script=document.createElement('script');
    script.type="text/javascript";
    if(typeof(callback)!="undefined"){
        if(script.readyState){
            script.onreadystatechange=function(){
            if(script.readyState == "loaded" || script.readyState == "complete")script.onreadystatechange=null;callback()}
        }else{
            script.onload=function(){callback()}
        }
    }
    script.src=url;
    document.body.appendChild(script);
}
function createXMLHttpRequest() {
    var xmlHttp;
    try{
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){}
        }
    }
    return xmlHttp;
}
function sheet2blob(sheet, sheetName) {
    sheetName = sheetName || 'sheet1';
    var workbook = {
        SheetNames: [sheetName],
        Sheets: {}
    }
    workbook.Sheets[sheetName] = sheet
    var wopts = {
        bookType: 'xlsx',
        bookSST: false,
        type: 'binary'
    }
    var wbout = XLSX.write(workbook, wopts);
    var blob = new Blob([s2ab(wbout)], {type:"application/octet-stream"});
    function s2ab(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
    return blob;
}
function openDownloadDialog(url, saveName){
    if(typeof url == 'object' && url instanceof Blob){
        url = URL.createObjectURL(url)
    }
    var aLink = document.createElement('a');
    aLink.href = url;
    aLink.download = saveName || '';
    var event;
    if(window.MouseEvent){
        event = new MouseEvent('click')
    }else{
        event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    }
    aLink.dispatchEvent(event);
}
function todo(url,num){
    var xml=createXMLHttpRequest()
    xml.open("GET", "https://weibo.com/ajax/statuses/show?id="+url.substring(url.lastIndexOf("/")+1), true);
    xml.send(null)
    xml.onreadystatechange = function() {
        if(xml.readyState == 4 && xml.status == 200) {
            var xml1=createXMLHttpRequest()
            var res=JSON.parse(xml.responseText)
            var title=res.user.screen_name+":"+res.text_raw
            var repost=res.reposts_count
            var comment=res.comments_count
            var like=res.attitudes_count
            xml1.open("GET","https://weibo.com/ajax/statuses/buildComments?&is_show_bulletin=2&count="+num+"&id="+JSON.parse(xml.responseText).mid, true);
            xml1.send(null)
            xml1.onreadystatechange = function() {
                if(xml1.readyState == 4 && xml1.status == 200) {
                    var result=JSON.parse(xml1.responseText).data
                    var aoa = [
                        [title,"转发"+repost,"评论:"+comment,"点赞："+like],
                        ['名字', '内容', '点赞', '回复'],
                    ]
                    for(var i=0;i<result.length;i++){
                        var res=result[i]
                        aoa.push(["@"+res.user.name,res.text.replace(/<[^>]+>/g,""),res.like_counts,res.total_number])
                    }
                    openDownloadDialog(sheet2blob(XLSX.utils.aoa_to_sheet(aoa)), title+'.xlsx');
                }
            }
        }
    }
}
loadJs("https://demo.haoji.me/2017/02/08-js-xlsx/js/xlsx.core.min.js",function(){
    todo("https://weibo.com/2099591797/KBL0IjhFu",100)
});